<template>
    <view class="container">
        <uni-nav-bar left-icon="left" :title="title" @clickLeft="backClick" background-color="#007AFF" dark></uni-nav-bar>
        <uni-forms ref="valiForm" :rules="rules" :modelValue="user" class="vcbox">
            <uni-forms-item label="登录名" name="username" required>
                <uni-easyinput v-model="user.username" placeholder="请输入学号、手机号或用户名"></uni-easyinput>
            </uni-forms-item>
            <uni-forms-item label="密码" name="password" required>
                <uni-easyinput v-model="user.password" type="password" placeholder="请输入密码"></uni-easyinput>
            </uni-forms-item>
        </uni-forms>
        <button type="primary" @click="doSubmit('valiForm')">登录</button>
        
        <!-- 添加三个注册按钮 -->
        <view class="register-buttons">
            <button type="default" @click="goToRegister('student')">学生注册</button>
            <button type="default" @click="goToRegister('teacher')">老师注册</button>
            <button type="default" @click="goToRegister('assistant')">助教注册</button>
            <button type="default" @click="goToRegister('admin')">管理员注册</button>
        </view>
    </view>
</template>

<script type="text/javascript">
// 导入 common.js，其中包含了 J 对象的定义
import "@/js/common.js";
// 导入 sha256 加密库
import sha256 from 'js-sha256';

export default {
    data() {
        return {
            title: '登录',
            user: { username: '', password: '' },
            rules: {
                username: {
                    rules: [{
                        required: true,
                        errorMessage: '登录名不能为空'
                    }]
                },
                password: {
                    rules: [{
                        required: true,
                        errorMessage: '密码不能为空'
                    }]
                }
            },
        }
    },
    methods: {
        backClick() {
            uni.navigateBack();
        },
        doSubmit(ref) {
            console.log('submit', ref);
            this.$refs[ref].validate().then(async res => {
                console.log(res, this.user);
                
                // 显示加载提示
                uni.showToast({
                    title: '登录中...',
                    icon: 'loading',
                    duration: 2000
                });
                
                try {
                    // 设置API地址
                    J.api.apiUrl = 'http://127.0.0.1:8089/__api__';
                    
                    // 调用登录API
                    var im = await J.callApiAsync('bigOAINet.UserControl.login', { 
                        username: this.user.username, 
                        password: sha256(this.user.password) 
                    });
                    
                    if (im.success) {
                        // 保存访问令牌
                        J.api.accessToken = im.data.accessToken;
                        J.uck.set('accessToken', im.data.accessToken, 1);
                        J.uck.set('userInfo', JSON.stringify(im.data), 1)
                        J.uck.set('userId', im.data.userId, 1)
                        // // 保存用户信息到本地存储
                        // uni.setStorageSync('userInfo', JSON.stringify(im.data));
                        // uni.setStorageSync('token', im.data.accessToken);
                        
                        console.log('登录成功:', im.data);
                        
                        uni.showToast({
                            title: '登录成功',
                            icon: 'success'
                        });
                        
                        // 登录成功后跳转到用户中心页面
                        setTimeout(() => {
                            uni.navigateTo({
                                url: '/pages/user/my'
                            });
                        }, 1500);
                    } else {
                        uni.showToast({
                            title: '登录失败: ' + im.msg,
                            icon: 'none'
                        });
                    }
                } catch (error) {
                    console.error('登录错误:', error);
                    uni.showToast({
                        title: '网络错误',
                        icon: 'none'
                    });
                }
            }).catch(err => {
                console.log('验证失败', err);
                uni.showToast({
                    title: '请检查输入',
                    icon: 'none'
                });
            });
        },
        // 跳转到对应类型的注册页面
        goToRegister(type) {
            switch(type) {
                case 'student':
                    uni.navigateTo({
                        url: '/pages/user/student-register'
                    });
                    break;
                case 'teacher':
                    uni.navigateTo({
                        url: '/pages/user/teacher-register'
                    });
                    break;
                case 'assistant':
                    uni.navigateTo({
                        url: '/pages/user/assistant-register'
                    });
                    break;
                case 'admin':
                    uni.navigateTo({
                        url: '/pages/user/admin-register'
                    });
                    break;
                default:
                    break;
            }
        }
    }
}
</script>

<style type="text/css">
.container{display: flex !important;flex-direction: column}
.content{display: flex !important;flex-direction: column;align-items:center;justify-content:center}
.title{font-size:36rpx;color:#8f8f94}
.vcbox{display: flex !important;flex-direction: column;align-items:center;justify-content:center;margin:15px;padding-left:10px;padding-right:10px}
button{margin: 15px;padding-left: 10px;padding-right: 10px;}

/* 注册按钮容器样式 */
.register-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 0 15px;
    margin-top: 10px;
}
</style>